﻿/* Muaz Khan – http://dashingquill.web.officelive.com */

using System;
using System.Web;
using DTweet.Models;
using System.Linq;

namespace DTweet.Helpers
{
    public static class CookieExtensions
    {
        public static void RemoveCookie(this string loginToken)
        {
            loginToken = loginToken.ToText();
            var cookieName = loginToken.Substring(0, 10);

            var request = HttpContext.Current.Request;
            var responce = HttpContext.Current.Response;

            if (request.Cookies[cookieName] == null) return;
            responce.Cookies.Remove(cookieName);
            var cookie = new HttpCookie(cookieName)
            {
                Expires = DateTime.Now.AddDays(-10) // according to MSDN, this is the only one way to remove cookies via asp.net
            };
            responce.Cookies.Add(cookie);
            responce.Cookies[cookieName].HttpOnly = true;
        }

        public static void AddCookie(this string loginToken)
        {
            loginToken = loginToken.ToText();
            var cookieName = loginToken.Substring(0, 10);
            var cookieValue = loginToken.Remove(0, 10);
            
            AddOrRemove(cookieName, cookieValue);           
        }
        private static void AddOrRemove(string cookieName, string cookieValue)
        {
            var request = HttpContext.Current.Request;
            var responce = HttpContext.Current.Response;

            if (!request.Browser.Cookies) return;

            if (request.Cookies[cookieName] == null)
            {
                cookieValue.AddCookie(responce, cookieName);
            }
            else if (request.Cookies[cookieName] != null)
            {
                responce.Cookies.Remove(cookieName);
                cookieValue.AddCookie(responce, cookieName);
            }
        }
        private static void AddCookie(this string value, HttpResponse responce, string cookieName)
        {
            var cookie = new HttpCookie(cookieName)
            {
                Value = value,
                Expires = DateTime.Now.AddYears(1)
            };
            responce.Cookies.Add(cookie);
            responce.Cookies[cookieName].HttpOnly = true;
        }

        /***********************************
         * Check whether user is logged in or ...
         ***********************************/
        public static void ValidateCookie(this DatabaseEntities db)
        {
            var request = HttpContext.Current.Request;
            if (!request.Browser.Cookies || request.Cookies.Count <= 0)
            {
                Static.InvalidReqeust();
                return;
            }

            var cookieName = request.Cookies[0].Name;
            var cookieValue = request.Cookies[0].Value;
            if (cookieName != null && cookieValue != null)
            {
                var loginToken = cookieName + cookieValue;
                loginToken = loginToken.ToNumbers();
                var user = db.Accounts.Where(a => a.LoginToken == loginToken).FirstOrDefault();
                
                if(user != null) user.ValidReqeust();
                else Static.InvalidReqeust();
            }
            else Static.InvalidReqeust();
        }
    }
}